package com.cmes.mes.basic.web.controller;

import com.cmes.core.utils.CollectionUtils;
import com.cmes.mes.basic.core.entity.MaterialCategoryEntity;
import com.cmes.mes.basic.core.service.MaterialCategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;

/**
 * 物料类型
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/mes/material/category")
public class MaterialCategoryController {

    private final MaterialCategoryService materialCategoryService;

    @PostMapping("tree")
    public List<MaterialCategoryEntity> tree() {
        return CollectionUtils.listTree(materialCategoryService.list(),
                0L,
                MaterialCategoryEntity::getId,
                MaterialCategoryEntity::getPid,
                MaterialCategoryEntity::setChildren,
                null,
                Comparator.comparing(MaterialCategoryEntity::getPid).thenComparing(MaterialCategoryEntity::getSort));
    }

    @PostMapping("selector")
    public List<MaterialCategoryEntity> selector() {
        return CollectionUtils.listTree(materialCategoryService.active(),
                0L,
                MaterialCategoryEntity::getId,
                MaterialCategoryEntity::getPid,
                MaterialCategoryEntity::setChildren,
                null,
                Comparator.comparing(MaterialCategoryEntity::getPid).thenComparing(MaterialCategoryEntity::getSort));
    }

}
